/*
	ONEGAME
    Copyright (C) 2009  Pablo Caballero

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

package one.project;

import java.io.File;
import java.io.FileWriter;
import java.net.MalformedURLException;
import java.net.URI;
import java.util.ResourceBundle;
import javax.swing.filechooser.FileFilter;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;

/**
 * Project that is saved in a XML
 * @author Pablo Caballero
 * @version 1.0 17-jun-2009
 */
public class ProjectXml extends AbstractProject {

	/**
	 * The file-filter
	 */
	private static FileFilter fileFilter = null;
	/**
	 * The path to the project file
	 */
	private URI path;

	public ProjectXml() {
	}

	/**
	 * Parses the document
	 * @param url URL to the document
	 * @return the document loaded
	 * @throws DocumentException if the document is wrong
	 * @throws MalformedURLException if the uri can be casted to url
	 */
	protected Document parse(URI uri) throws DocumentException, MalformedURLException {
		SAXReader reader = new SAXReader();
		Document document = reader.read(uri.toURL());
		return document;
	}

	/**
	 * Creates a dom document
	 * @return the dom document
	 */
	protected Document createDocument() {
		Document document;
		Element e_root, e_author;
		ISystemData sysdata;

		document = DocumentHelper.createDocument();
		e_root = document.addElement("project");

		sysdata = getSystemData();
		e_author = e_root.addElement("author");
		e_author.addText(sysdata.getAuthor());

		return document;
	}

	public void load() throws Exception {
		Document doc;
		ISystemData sysdata;

		doc = parse(getPath());

		//TODO: LOADING...
		sysdata = getSystemData();
		sysdata.setAuthor("");
	}

	/**
	 * Loads the project
	 * @throws java.lang.Exception If an error was produced on loading
	 */
	public void save() throws Exception {
		Document doc;
		XMLWriter writer;

		doc = createDocument();
		writer = new XMLWriter(new FileWriter(new File(getPath())));
		writer.write(doc);
		writer.close();
	}

	public FileFilter getFileFilter() {
		if (fileFilter == null) {
			fileFilter = new FileFilterProject();
		}
		return fileFilter;
	}

	public URI getPath() {
		return path;
	}

	public void setPath(URI uri) {
		path = uri;
	}

	/**
	 * File-Filter project
	 */
	protected class FileFilterProject extends FileFilter {

		/**
		 * The description
		 */
		private String description;

		/**
		 * Cosntructor
		 */
		public FileFilterProject() {
			ResourceBundle bundle = ResourceBundle.getBundle("one/language/labels"); // NOI18N
			description = bundle.getString("PROJECT_FILE_DESCRIPTION"); // NOI18N
		}

		/**
		 *
		 * @param f The file to be accepted or refused
		 * @return
		 */
		@Override
		public boolean accept(File f) {
			String name = f.getName();
			return name.endsWith(".prj") || name.endsWith(".xml");
		}

		/**
		 * The description of the filter
		 * @return the description
		 */
		@Override
		public String getDescription() {
			return description;
		}
	}
}
